#include "caredit.h"
#include "ui_caredit.h"
#include<QDebug>
caredit::caredit(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::caredit)
{
    ui->setupUi(this);
}
caredit::caredit(QString user,QString flag){
    this->user=user;
    this->flag=flag;
}
caredit::~caredit()
{
    delete ui;
}
//设置当前用户
void caredit::setuser(QString username){
    this->user=username;
}
//设置操作用户列席
void caredit::setflag(QString flag){
    this->flag=flag;
}
void caredit::on_pushButton_6_clicked()
{
    this->close();
}
void caredit::setread(){
    if(flag=="用户"){//如果是用户修改汽车所属类型只可读
        ui->lineEdit_7->setText(user);
        ui->lineEdit_7->setReadOnly(true);
    }
}

void caredit::on_pushButton_2_clicked()
{
    this->close();
}

void caredit::on_pushButton_4_clicked()
{
    this->close();
}
//汽车修改
void caredit::on_pushButton_clicked()
{
    QString carid1 = ui->lineEdit->text();//原汽车
    QString carid2 = ui->lineEdit_2->text();//修改后车牌
    QString cartype = ui->comboBox_2->currentText();//类型
    if(carid1.isEmpty() ||carid2.isEmpty() || cartype.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不能为空"), QMessageBox::Yes);
    }
    else{
        ui->lineEdit->clear();
        ui->lineEdit_2->clear();
        QSqlQuery query;
        QString str = QString("update carinfo set carid='%1',cartype='%2' where carid='%3';").arg(carid2).arg(cartype).arg(carid1);
        if(query.exec(str)){
            QMessageBox::warning(nullptr, QStringLiteral("提示"), QStringLiteral("修改成功"), QMessageBox::Yes);
            this->close();//关闭返回用户界面;
        }
        else
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("修改失败"), QMessageBox::Yes);
    }
}
//汽车增加
void caredit::on_pushButton_3_clicked()
{

    QString carid = ui->lineEdit_4->text();//车牌
    QString cartype = ui->comboBox->currentText();//类型
    if(carid.isEmpty()|| cartype.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不能为空"), QMessageBox::Yes);
    }
    else{

        ui->lineEdit_4->clear();

        QSqlQuery query;
        QString str = QString("insert into carinfo(id,carid,cartype,username) values(0, '%1', '%2','%3')").arg(carid).arg(cartype).arg(user);
        if(query.exec(str)){
            QMessageBox::warning(nullptr, QStringLiteral("提示"), QStringLiteral("增加成功"), QMessageBox::Yes);
            this->close();//关闭返回用户界面;
        }
        else
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("增加失败"), QMessageBox::Yes);
    }
}
//删除汽车
void caredit::on_pushButton_5_clicked()
{
    QString carid = ui->lineEdit_6->text();//车牌
    if(carid.isEmpty()){
        QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("输入的信息不能为空"), QMessageBox::Yes);
    }
    else{
        ui->lineEdit_6->clear();
        QSqlQuery query;
        QString str = QString("delete from carinfo where carid='%1';").arg(carid);
        if(query.exec(str)){
            QMessageBox::warning(nullptr, QStringLiteral("提示"), QStringLiteral("删除成功"), QMessageBox::Yes);
            this->close();//关闭返回用户界面;
        }
        else
            QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("删除失败"), QMessageBox::Yes);
    }
}
